package com.project.myPackage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.myPackage.entity.Products;
import com.project.myPackage.mapper.ProductsMapper;
import com.project.myPackage.service.ProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class ProductsServiceImpl extends ServiceImpl<ProductsMapper, Products> implements ProductsService {
  @Autowired
  private ProductsMapper productsMapper;

    @Override
    public List<Products> listByConditions(Integer category_id, String product_name, Integer status) {
        QueryWrapper<Products> queryWrapper = new QueryWrapper<>();
        if (category_id != null) {
            queryWrapper.eq("category_id", category_id);
        }
        if (product_name != null && !product_name.isEmpty()) {
            queryWrapper.like("product_name", product_name);
        }
        if (status != null) {
            queryWrapper.eq("status", status);
        }
        return list(queryWrapper);
    }

   public List<Products> getRecommendProductsByUserId(Integer user_id){
       // 获取用户收藏的所有商品分类
       List<Integer> favoriteCategories = productsMapper.getFavoriteCategoriesByUserId(user_id);

       // 根据分类列表获取所有商品中的这些分类的商品列表
       Set<Integer> categoryIds = favoriteCategories.stream().collect(Collectors.toSet());

       // 检查 categoryIds 是否为空
       if (categoryIds.isEmpty()) {
           // 如果 categoryIds 为空，直接返回一个空列表
           return new ArrayList<>();
       }

       return productsMapper.getProductsByCategoryIds(categoryIds);
   }
}
